<?php

namespace app\modules\api\controllers;

use app\components\Func;
use app\models\Qun;
use app\modules\api\CommonController;
use Yii;


class QunController extends CommonController
{
    //附近的群
    public function actionNearObjs()
    {
        $keyword = Yii::$app->request->post('keyword', 'a');
        $lat = Yii::$app->request->post('lat', 0);
        $lng = Yii::$app->request->post('lng', 0);
        $distance = Yii::$app->request->post('distance', 100000);
        if(empty($lat)) throw new \Exception('经度不能为空',1);
        if(empty($lng)) throw new \Exception('纬度不能为空',1);
        if($keyword){
            $objs = Qun::find()->andWhere(['like','title',$keyword])->all();
        }else{
            $objs = Qun::getNearObjs($lng, $lat, $distance);
        }
//        $objs = Qun::find()->where(1)->all();
//        print_r($objs);exit;
        $arr = [];
        foreach ($objs as $obj){
            $distance = Func::countGpsDistance($lng,$lat,$obj->lng,$obj->lat);
            $arr[] = [
                'id' => $obj->id,
                'title' => $obj->title,
                'lng' => $obj->lng,
                'lat' => $obj->lat,
                'ctime' => $obj->ctime,
                'ctime2' => date('Y-m-d H:i:s',$obj->ctime),
                'distance_value' => $distance,
                'distance' => Func::distanceFormat($distance),
            ];
        }
        usort($arr,function($a,$b){
            if ($a['distance_value'] == $b['distance_value']) {
                return 0;
            }
            return ($a['distance_value'] < $b['distance_value']) ? -1 : 1;
        });
        return $this->success(['rows' => $arr]);
    }
    //群详情
    public function actionInfo(){
        $id = Yii::$app->request->post('id', 0);
        $qun = Qun::findOne($id);
        if(empty($qun)) throw new \Exception('群为空',1);
        return $this->success(['row' => [
            'id' => $qun->id,
            'title' => $qun->title,
            'lng' => $qun->lng,
            'lat' => $qun->lat,
            'pic' => $qun->picObj->getFileUrl(),
            'ctime' => $qun->ctime,
            'ctime2' => date('Y-m-d H:i:s',$qun->ctime),
            'hide_del_button' => $qun->uid != $this->user->id,
        ]]);
    }
    //群详情
    public function actionDel(){
        $id = Yii::$app->request->post('id', 0);
        $qun = Qun::findOne($id);
        if(empty($qun)) throw new \Exception('群为空',1);
        if($qun->uid != $this->user->id){
            throw new \Exception('无权删除',1);
        }
        $qun->delete();
        return $this->success([]);
    }
    //我的群
    public function actionMyObjs(){
//        $objs = Qun::find()->andWhere(['uid'=>$this->uid])->all();
        $objs = Qun::find()->where(['uid'=>$this->user->id])->all();
//        print_r($objs);exit;
        $rows = [];
        foreach ($objs as $obj){
            $rows[] = [
                'id' => $obj->id,
                'title' => $obj->title,
                'lng' => $obj->lng,
                'lat' => $obj->lat,
                'ctime' => $obj->ctime,
                'ctime2' => date('Y-m-d H:i:s',$obj->ctime),
            ];
        }
        return $this->success(['rows' => $rows]);
    }
    //群添加保存
    public function actionAddSave(){
        $title = Yii::$app->request->post('title', '');
        $lat = Yii::$app->request->post('lat', 0);
        $lng = Yii::$app->request->post('lng', 0);
        $upload_ids = Yii::$app->request->post('upload_ids', 0);
        if(empty($title)) throw new \Exception('群名不能为空',1);
        if(empty($lat)) throw new \Exception('经度不能为空',1);
        if(empty($lng)) throw new \Exception('纬度不能为空',1);
        if(empty($upload_ids)) throw new \Exception('图片不能为空',1);
        $upload_ids = explode(',',$upload_ids);

        $qun = new Qun();
        $qun->uid = $this->user->id;
        $qun->title = $title;
        $qun->lat = $lat;
        $qun->lng = $lng;
        $qun->pic = $upload_ids[0];
        $qun->ctime = time();
        if(!$qun->save())throw new \Exception(json_encode($qun->getErrors(),JSON_UNESCAPED_UNICODE),1);
        return $this->success();
    }
}
